package com.ezjie.ielts.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ezjie.ielts.core.helper.AppDataBaseHelper;
import com.ezjie.login.model.UserDetail;

public class UserInfoDB {
	private static AppDataBaseHelper dbHelper;
	private SQLiteDatabase db;

	public UserInfoDB(Context context) {
		dbHelper = AppDataBaseHelper.getInstance(context);
	}

	// 查找用户
	public synchronized List<UserDetail> query() {
		db = dbHelper.getReadableDatabase();
		List<UserDetail> infos = new ArrayList<UserDetail>();
		Cursor cursor = null;
		try {
			db.beginTransaction();// 开始事务
			String sql = "select * from " + AppDataBaseHelper.USERINFO;
			cursor = db.rawQuery(sql, null);
			while (cursor.moveToNext()) {
				UserDetail info = new UserDetail();
				info.uid = cursor.getString(1);
				info.nick_name = cursor.getString(2);
				info.mobile = cursor.getString(3);
				info.email = cursor.getString(4);
				info.sex = cursor.getString(5);
				info.head_url = cursor.getString(6);
				info.login_key = cursor.getString(7);
				info.islogin = cursor.getInt(8);
				infos.add(info);
			}
			db.setTransactionSuccessful();// 设置事务的标志为True
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.endTransaction();// 结束事务,有两种情况：commit,rollback,事务的提交或回滚是由事务的标志决定的,如果事务的标志为True，事务就会提交，否侧回滚,默认情况下事务的标志为False
			cursor.close();
			db.close();
		}

		return infos;
	}

	// 查找当前登陆用户 没有就返回一个默认对象。
	public synchronized UserDetail queryLoginUser() {
		db = dbHelper.getReadableDatabase();
		UserDetail info = new UserDetail();
		List<UserDetail> lists = query();
		for (int i = 0; i < lists.size(); i++) {
			if (lists.get(i).islogin == 1) {
				info = lists.get(i);
				return info;
			}
		}

		return info;
	}

	/**
	 * 用户是否已经存在
	 * 
	 * @param uid
	 * @return
	 */
	public synchronized boolean isExistUid(String uid) {
		boolean isExist = false;
		db = dbHelper.getReadableDatabase();
		String sql = "select * from " + AppDataBaseHelper.USERINFO + " where uid=?";
		Cursor cursor = db.rawQuery(sql, new String[] { uid });
		if (cursor.moveToNext()) {
			isExist = true;

		} else {
			isExist = false;
		}
		cursor.close();
		db.close();

		return isExist;
	}

	// 删除用户
	public synchronized void delete() {
		db = dbHelper.getWritableDatabase();

		String sql = "delete from " + AppDataBaseHelper.USERINFO;
		db.execSQL(sql);
		db.close();
	}

	// 更新用户
	public synchronized void update(UserDetail userInfo) {
		db = dbHelper.getWritableDatabase();

		String sql = "update " + AppDataBaseHelper.USERINFO
				+ " set nick_name=?,mobile=?,email=?,sex=?,head_url=?,login_key=?,islogin=? where uid=?";
		Object[] objs = new Object[] { userInfo.nick_name, userInfo.mobile, userInfo.email, userInfo.sex,
				userInfo.head_url, userInfo.login_key, userInfo.islogin, userInfo.uid };
		db.execSQL(sql, objs);
		db.close();
	}

	/**
	 * 插入用户
	 * 
	 * @param category
	 */
	public synchronized void insert(UserDetail userInfo) {
		db = dbHelper.getWritableDatabase();

		// db.beginTransaction();
		String sql = "insert into " + AppDataBaseHelper.USERINFO
				+ " (uid,nick_name,mobile,email,sex,head_url,login_key,islogin) " + "values(?,?,?,?,?,?,?,?)";
		db.execSQL(sql, new Object[] { userInfo.uid, userInfo.nick_name, userInfo.mobile, userInfo.email, userInfo.sex,
				userInfo.head_url, userInfo.login_key, userInfo.islogin });
		db.close();

	}

	/**
	 * 更新用户
	 * 
	 * @param category
	 */
	public synchronized void replaceInto(UserDetail userInfo) {
		db = dbHelper.getWritableDatabase();

		String sql = "replace into " + AppDataBaseHelper.USERINFO
				+ " (uid,nick_name,mobile,email,sex,head_url,login_key,islogin) " + "values(?,?,?,?,?,?,?,?)";
		db.execSQL(sql, new Object[] { userInfo.uid, userInfo.nick_name, userInfo.mobile, userInfo.email, userInfo.sex,
				userInfo.head_url, userInfo.login_key, userInfo.islogin });

		db.close();

	}
}
